package com.example.video.controller;

import com.example.video.common.Result;
import com.example.video.entity.Platform;
import com.example.video.service.impl.PlatformServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 *
 * @author qiusuyang
 * @since 2024-04-17
 */
@Controller
@RequestMapping("/platform")
public class PlatformController {

    @Resource
    private PlatformServiceImpl platformService;



    /**
     * @desc 通过页数和大小获取目标账号列表
     * @param page 页数
     * @param limit 每页大小
     * @return 获取的账户列表
     */
    @ResponseBody
    @PostMapping("/platformList")
    public Result<List<Platform>> getPlatformsList(Integer page, Integer limit){
        Result<List<Platform>> rs = new Result<>();
        List<Platform> list = platformService.getList(page, limit) ;
        if( list.isEmpty() ){
            rs.setCode(-1);
            rs.setData(null);
            rs.setMsg("获取失败！");
        }else{
            rs.setCode(0);
            rs.setData(list);
            rs.setMsg("获取成功！");
        }
        rs.setCount(platformService.GetCount());
        return rs;
    }

    @ResponseBody
    @PostMapping("/add")
    public Result<Platform> add(Platform platform){
        Result<Platform> rs = new Result<>();
        if( platformService.add(platform) ){
            rs.setCode(0);
            rs.setData(platform);
            rs.setMsg("添加成功！");
        }else{
            rs.setCode(-1);
            rs.setData(null);
            rs.setMsg("添加失败！");
        }
        return rs;
    }

    @ResponseBody
    @PostMapping("/modify")
    public Result<Platform> modify(Platform platform){
        Result<Platform> rs = new Result<>();
        if( platformService.modify(platform) ){
            rs.setCode(0);
            rs.setData(platform);
            rs.setMsg("修改成功！");
        }else{
            rs.setCode(-1);
            rs.setData(null);
            rs.setMsg("修改失败！");
        }
        return rs;
    }

    @ResponseBody
    @PostMapping("/delete")
    public Result<Platform> delete(Integer id){
        Result<Platform> rs = new Result<>();
        if( platformService.delete(id) ){
            rs.setCode(0);
            rs.setData(null);
            rs.setMsg("删除成功！");
        }else{
            rs.setCode(-1);
            rs.setData(null);
            rs.setMsg("删除失败！");
        }
        return rs;
    }

    /**
     * @desc 获取全部平台列表
     * @return 获取的账户列表
     */
    @ResponseBody
    @PostMapping("/platformAll")
    public Result<List<Platform>> getPlatformsAll(){
        Result<List<Platform>> rs = new Result<>();
        List<Platform> list = platformService.getAll() ;
        if( list.isEmpty() ){
            rs.setCode(-1);
            rs.setData(null);
            rs.setMsg("获取失败！");
        }else{
            rs.setCode(0);
            rs.setData(list);
            rs.setMsg("获取成功！");
        }
        rs.setCount(list.size());
        return rs;
    }

}
